package com.test.gpa.security.config;

import com.test.gpa.security.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    /**
     * 加密方式 配置bean
     */
    public PasswordEncoder passwordEncoder() {//配置效验密码方式
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder e = new BCryptPasswordEncoder();

        System.out.println(e.encode("746228272"));
        System.out.println(e.upgradeEncoding("$2a$10$KAF3t9F0fu1/h/UyQa/xkuurpDc5OvupphF3BVOtMGpnVTbvdV2te"));
    }

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    AuthenticationEntryPoint authenticationEntryPoint; //自定义的异常处理配置成bean
    @Autowired
    AccessDeniedHandler accessDeniedHandler;
    @Autowired
    private AuthenticationSuccessHandler successHandler;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private AuthenticationFailureHandler failureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()//关闭csrf
                .disable()
                .sessionManagement()//不通过Session获取SecurityContext
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().authorizeRequests()// 对于登录接口 允许匿名访问
                .antMatchers("/user/login", "/logout")
                .anonymous()// 匿名访问,除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();;

        //将token过滤器加到 security 过滤器链中，放到 UserNamepasswordAuthenticationFilterqian
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //配置认证+授权异常返回机制
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        http.formLogin().successHandler(successHandler).failureHandler(failureHandler);//认证成功处理器,认证失败处理器
        http.logout().logoutSuccessHandler(logoutSuccessHandler);//配置注销成功处理器

        http.cors();//允许跨域
    }

    /***
     * 将AuthenticationManager 配置到容器中，使用authenticationmanager 对用户进行验证；
     * * @return * @throws Exception */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
